package com.bjc.sdx.activity;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.os.Bundle;
import android.widget.FrameLayout;

import androidx.appcompat.app.AppCompatActivity;

import com.bjc.sdx.R;
import com.bjc.sdx.utils.ThreadPoolManager;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

public class FrameActivity extends AppCompatActivity {

    private Timer timer;
    FrameLayout frameLayout;
    Drawable drawable;
    Handler handler = new Handler(){
        int i = 0;
        @Override
        public void handleMessage(Message msg) {
            if (msg.what==1){
                i++;
                move(i%4);
            }
            super.handleMessage(msg);
        }
    };
    void move(int i){
        drawable = getResources().getDrawable(R.mipmap.ic_launcher,null);
        Drawable drawable1 = getResources().getDrawable(R.mipmap.ic_launcher_round,null);
        Drawable drawable2 = getResources().getDrawable(R.drawable.people,null);
        Drawable drawable3 = getResources().getDrawable(R.drawable.ic_baseline_star_24,null);
        switch (i){
            case 0:
                frameLayout.setForeground(drawable);
                break;
            case 1:
                frameLayout.setForeground(drawable1);
                break;
            case 2:
                frameLayout.setForeground(drawable2);
                break;
            case 3:
                frameLayout.setForeground(drawable3);
                break;
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_frame);
        frameLayout = (FrameLayout)findViewById(R.id.fl);
        timer = new Timer();
//    timer.schedule(new TimerTask() {
//        @Override
//        public void run() {
//            handler.sendEmptyMessage(1);
//        }
//    },0,500);//第二个参数是隔多少秒之后开始显示，第三个是隔多久显示下一个

        ThreadPoolManager.getInstance()
                .getScheduledExcutorService()
                .scheduleAtFixedRate(new Runnable() {
                    @Override
                    public void run() {
                        handler.sendEmptyMessage(1);
                    }
                },0,1000, TimeUnit.MILLISECONDS);//第二个参数是隔多少秒之后开始显示，第三个是隔多久显示下一个
    }


    @Override
    protected void onDestroy() {
        timer.cancel();
        super.onDestroy();

    }
}
